package com.elitel.frame.base.service.impl;

import com.elitel.common.utils.UUIDUtil;
import com.elitel.frame.base.service.ApplicationServiceRelationService;
import com.elitel.frame.main.dao.AcAppMapper;
import com.elitel.frame.main.dao.AcAppServiceMapper;
import com.elitel.frame.main.entity.AcApp;
import com.elitel.frame.main.entity.AcAppService;
import com.elitel.frame.main.entity.AcAppServiceRelation;
import com.elitel.frame.main.entity.vo.AcAppServiceRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author hi_bj
 */
@Service
public class ApplicationServiceRelationServiceImpl implements ApplicationServiceRelationService {

    /** null */
    private final String[] EMPTY = new String[]{};

    private String curName = "admin";
    @Resource
    AcAppMapper acAppMapper;

    @Resource
    AcAppServiceMapper acAppServiceMapper;

    @Override
    public List<AcAppServiceRelation> getDataList(AcAppServiceRelation acAppServiceRelation) {
        List<AcAppServiceRelation> resultList = new ArrayList<>();
        AcApp acApp = new AcApp();
        BeanUtils.copyProperties(acAppServiceRelation, acApp);
        List<AcApp> acAppList = acAppMapper.getAll(acApp);
        List<AcAppService> acAppServiceList = acAppServiceMapper.getAllByServiceId(acAppServiceRelation.getServiceId());
        //处理应用和服务关联关系
        acAppList.forEach(app -> {
            AcAppServiceRelation appServiceRelation = new AcAppServiceRelation();
            BeanUtils.copyProperties(app, appServiceRelation);
            resultList.add(appServiceRelation);
            acAppServiceList.forEach(service -> {
                if (app.getAppId().equals(service.getAppId())) {
                    appServiceRelation.setAppServiceId(service.getAppServiceId());
                    appServiceRelation.setServiceId(service.getServiceId());
                }
            });
        });
        return resultList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(AcAppServiceRelationVo acAppServiceRelationVo) {
        int count = 0;
        //1.移除旧数据
        String[] removeAppServiceIds = ObjectUtils.isEmpty(acAppServiceRelationVo.getRemoveAppServiceId()) ?
                EMPTY : acAppServiceRelationVo.getRemoveAppServiceId().split(",");
        for (String appServiceId : removeAppServiceIds) {
            count += acAppServiceMapper.deleteByPrimaryKey(appServiceId);
        }
        //2.添加新数据
        String[] addAppIds = ObjectUtils.isEmpty(acAppServiceRelationVo.getAddAppId()) ?
                EMPTY : acAppServiceRelationVo.getAddAppId().split(",");
        for (String appId : addAppIds) {
            AcAppService acAppService = new AcAppService();
            acAppService.setAppServiceId(UUIDUtil.randomUUID());
            acAppService.setServiceId(acAppServiceRelationVo.getServiceId());
            acAppService.setAppId(appId);
            acAppService.setCreatedTime(new Date());
            acAppService.setCreatedUser(curName);
            count += acAppServiceMapper.insertSelective(acAppService);
        }
        //3.更新应用表服务数据量
        String[] removeAppIds = ObjectUtils.isEmpty(acAppServiceRelationVo.getRemoveAppId()) ?
                EMPTY :acAppServiceRelationVo.getRemoveAppId().split(",");
        String[] appIds = Stream.of(addAppIds, removeAppIds).flatMap(Arrays::stream).toArray(String[]::new);
        for (String appId : appIds) {
            List<AcAppService> acAppServiceListByAppId = acAppServiceMapper.getAllByAppId(appId);
            AcApp acApp = new AcApp();
            acApp.setAppId(appId);
            acApp.setServiceCount(acAppServiceListByAppId.size());
            acAppMapper.updateByPrimaryKeySelective(acApp);
        }

        return count;
    }

}
